<?php
namespace Admin\Form\Category;

use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

use Admin\Mapper\Category\AdminCategory;


class CreateCategoryFieldset extends Fieldset implements InputFilterProviderInterface
{
    protected $serviceLocator;
    protected $categoryMapper;

    public function __construct(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;

        parent::__construct('create_category');

        $this
            ->setHydrator(new ClassMethods())
            //->setObject(new UserEntity())
        ;

        $this->add(array(
            'type' => 'Admin\Form\Category\CreateCategoryDescriptionFieldset',
            'options' => array(
                'use_as_base_fieldset' => true,
            ),
        ));


        $this->add(array(
            'type' => 'select',
            'name' => 'location',
            'required' => true ,
            'filters'  => array(
                array('name' => 'Int'),
            ),
            'options' => array(
                'label' => 'Location',
                'value_options' => $this->optionCategory(), /*array(
                    '0' => 'Root',
                ),*/
            ),
            'attributes' => array(
                'class' => 'form-control col-xs-12',
            ),
        ));


        $this->add(array(
            'name' => 'status',
            'escape' => true ,
            'type' => 'radio',
            'options' => array(
                'value_options' => array(
                    'A' => ' Active',
                    'H' => ' Hidden',
                    'D' => ' Disabled',
                ),
                'label_attributes' => array(
                    'class' => 'radio-inline'
                )
            ),
        ));

        $this->add(array(
            'name' => 'category_id',
            'type' => 'Zend\Form\Element\Hidden',
            'attributes' => array(
                'type' => 'hidden'
            ),
        ));

    }

    /**
     * @return array
     */
    public function getInputFilterSpecification() {
        return array(
            'name' => array(
                'name'       => 'name',
                'required'   => true,
                'filters' => array(
                    array('name' => 'StripTags') ,
                    array('name' => 'StringTrim') ,
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength' ,
                        'options' => array(
                            'encoding' => 'UTF-8' ,
                            'min' => 2 ,
                            'max' => 50 ,
                        ) ,
                    ),
                ),
            ),

            'location' => array(
                'name'       => 'location',
                'required'   => true,
                'filters' => array(
                    array('name' => 'StripTags') ,
                    array('name' => 'StringTrim') ,
                ),
                'validators' => array(
                    array(
                        'name'                   => 'Digits',
                        'break_chain_on_failure' => true,
                    ),
                ),
            ),
        );
    }

    public function optionCategory()
    {
        $categories = $this->getCategoryMapper()->fetchAll();
        foreach($categories as $category)
        {
            //die(print_r($category));
            $data[] = $category->getCategoryId();
        }

        return $data;
    }

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

    public function getCategoryMapper()
    {
        if(!$this->categoryMapper)
        {
            $this->setCategoryMapper($this->getServiceLocator()->get('admin_category_mapper'));
        }
        return $this->categoryMapper;
    }

    public function setCategoryMapper(AdminCategory $categoryMapper)
    {
        $this->categoryMapper = $categoryMapper;
        return $this;
    }

} 